#ifndef SWP_DECISION_GRAPHIC_ITEM_H
#define SWP_DECISION_GRAPHIC_ITEM_H

#include "abstractpolygongraphicitem.h"

#include <rectgraphicitem.h>

namespace gui
{
    class LineEditDelegate;
}

namespace swp
{

class SwpDecisionGraphicItem : public AbstractPolygonGraphicItem
{
private:
    class Label : public gui::RectGraphicItem
    {
    public:
        Label(const QString& text, SwpDecisionGraphicItem* parent = 0);

        QString text() const;
        void setText(const QString& text);

    private:
        void relayout();

    private:
        QGraphicsSimpleTextItem* m_textItem;
    };

public:
    SwpDecisionGraphicItem(QGraphicsItem* parent = 0);
    ~SwpDecisionGraphicItem();

    void handleConnectorPositionChange();
    void connectionsRefreshed();

    IEditorItem* yesBranchItem() const;
    IEditorItem* noBranchItem() const;

    // IEditorItem
    virtual bool supportsConnection(
        const core::ConnectionData& connection) const;

    virtual int type() const;

    QString name() const;

    virtual IEditorItem* copy() const;

    // IPropertyObject
    virtual QList<core::Property> properties() const;

    virtual QVariant propertyValue(const QString& propertyName) const;

    virtual void propertyChanged(const QString& property,
        const QVariant& newValue, bool silent = false);

    virtual QVariantList optionList(const QString& propertyName) const;
    virtual QVariantList autocomplete(const QString& propertyName) const;

protected:
    // AbstractPolygonGraphicItem
    QList<QPointF> scalePoints() const;
    QList<QPointF> connectPoints() const;
    QList<QPointF> selfConnectAnchorPoints() const;

private:
    bool containsSameConnection(const core::ConnectionData& connection,
        const QList<core::ConnectionData>& connections) const;
    int incomingConnectionSize(
        const QList<core::ConnectionData>& connections) const;
    int outgoingConnectionSize(
        const QList<core::ConnectionData>& connections) const;
    void changeConnectionLabel(const QPointF& connectPoint,
        const QString& label, IEditorItem*& connectorItem,
        IEditorItem*& connecteeItem);
    void resetConnectionLabel(IEditorItem*& connectorItem,
        IEditorItem*& connecteeItem);
    void changeLabel(const QString& label);
    void changeLabelPosition(const QPointF& connectPoint);

protected:
    core::Property m_labelProp;
    core::Property m_labelPositionProp;
    core::Property m_yesProp;
    core::Property m_noProp;
    QHash<QString, QPointF> m_connectPointToDirectionMapping;
    IEditorItem* m_yesPropConnector;
    IEditorItem* m_noPropConnector;
    IEditorItem* m_yesBranchItem;
    IEditorItem* m_noBranchItem;
    Label* m_label;
    gui::LineEditDelegate* m_labelDelegate;
};

}

#endif
